package database

import (
	"alfred/models"
	"time"
)

// CreateNotificationConfig 创建NotificationConfig
func (db *DB) CreateNotificationConfig(config *models.NotificationConfig) error {
	config.ID = models.NewID()
	config.CreatedAt = time.Now()
	config.UpdatedAt = time.Now()

	return db.Create(config).Error
}

// GetNotificationConfig 获取NotificationConfig
func (db *DB) GetNotificationConfig(id string) (*models.NotificationConfig, error) {
	var config models.NotificationConfig
	err := db.First(&config, "id = ?", id).Error
	if err != nil {
		return nil, err
	}
	return &config, nil
}

// GetNotificationConfigs 获取所有启用的通知配置
func (db *DB) GetNotificationConfigs() ([]*models.NotificationConfig, error) {
	var configs []*models.NotificationConfig
	err := db.Where("enabled = ?", true).Find(&configs).Error
	return configs, err
}

// ListNotificationConfigs 获取所有NotificationConfig
func (db *DB) ListNotificationConfigs() ([]*models.NotificationConfig, error) {
	var configs []*models.NotificationConfig
	err := db.Order("created_at DESC").Find(&configs).Error
	return configs, err
}

// UpdateNotificationConfig 更新NotificationConfig
func (db *DB) UpdateNotificationConfig(config *models.NotificationConfig) error {
	config.UpdatedAt = time.Now()
	return db.Save(config).Error
}

// DeleteNotificationConfig 删除NotificationConfig
func (db *DB) DeleteNotificationConfig(id string) error {
	return db.Delete(&models.NotificationConfig{}, "id = ?", id).Error
}